import { defineStore } from "pinia";
import { ref } from "vue";
import { ResponseStatus } from "@/api/request";
import { getConfiguration, type Configuration } from "@/api/configuration";

export const useConfigurationStore = defineStore("configuration", {
  state() {
    return {
      configuration: undefined as Partial<Configuration> | undefined,
    };
  },
  actions: {
    async getConfiguration() {
      if (this.configuration) {
        return this.configuration;
      }
      const result = await getConfiguration();
      if (result.status === ResponseStatus.SUCCESS) {
        this.configuration = result.data;
      }
      return this.configuration;
    },
  },
});

export const useUserStore = defineStore("user", () => {
  const login = ref(false);
  const information = ref({
    avatar: "",
    nickname: "",
    username: "",
    email: "",
    social: {
      gitee: "",
      github: "",
      twitter: "",
    },
    role: "",
    createdAt: "",
    updatedAt: "",
    lastLoginAt: "",
  });

  return { login, information };
});

export type UserStore = ReturnType<typeof useUserStore>;
